package week2.hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class BaseWork {
    public static void main(String[] args) throws IOException {
        //建立连接
        Configuration conf = new HBaseConfiguration().create();
        conf.set("hbase.zookeeper.quorum","emr-worker-2,emr-worker-1,emr-header-1");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //创建表
        TableName tableName = TableName.valueOf("shijian:student");
        String[] colFamily = new String[]{"name", "info", "score"};
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        for (int i = 0; i < 3; i++) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(colFamily[i]);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);
        //插入数据
        Put put = new Put(Bytes.toBytes("Tom"));
        put.addColumn(Bytes.toBytes(colFamily[1]), Bytes.toBytes("student_id"), Bytes.toBytes(202101));
        put.addColumn(Bytes.toBytes(colFamily[1]), Bytes.toBytes("class"), Bytes.toBytes(1));
        put.addColumn(Bytes.toBytes(colFamily[2]), Bytes.toBytes("understanding"), Bytes.toBytes(75));
        put.addColumn(Bytes.toBytes(colFamily[2]), Bytes.toBytes("programing"), Bytes.toBytes(82));
        conn.getTable(tableName).put(put);

        put = new Put(Bytes.toBytes("Shijian"));
        put.addColumn(Bytes.toBytes(colFamily[1]), Bytes.toBytes("student_id"), Bytes.toBytes("G20220735030047"));
        put.addColumn(Bytes.toBytes(colFamily[1]), Bytes.toBytes("class"), Bytes.toBytes(2));
        put.addColumn(Bytes.toBytes(colFamily[2]), Bytes.toBytes("understanding"), Bytes.toBytes(90));
        put.addColumn(Bytes.toBytes(colFamily[2]), Bytes.toBytes("programing"), Bytes.toBytes(92));
        conn.getTable(tableName).put(put);
        /*查看数据
        Get get = new Get(Bytes.toBytes("Shijian"));
        if(!get.isCheckExistenceOnly()){
            Result result = conn.getTable(tableName).get(get);
            for (Cell cell:result.rawCells()) {
                String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
            }
        }

         */
        //删除数据
        //删除表

    }
}
